<template>
  <div class="login-modal">
    <div class="mask" @click="showLogin"></div>
    <div class="loginBox">
      <div class="loginIn">
        <ul class="scanCode">
          <li :class="isShowForm ? 'active' : ''" @click="isShowForm = true">
            手机扫码登录
          </li>
          <li>|</li>
          <li :class="!isShowForm ? 'active' : ''" @click="weixinClick">
            微信扫码登录
          </li>
        </ul>
        <form v-show="isShowForm">
          <div class="phoneInput">
            <input
              type="text"
              placeholder="请输入手机号"
              v-model="phoneValue"
            />
          </div>
          <!-- <div class="slideBox">
            <p>向右滑动填充拼图</p>
            <div class="slide"></div>
          </div> -->
          <slide-verify
            :l="42"
            :r="20"
            :w="362"
            :h="140"
            @success="onSuccess"
            @fail="onFail"
            @refresh="onRefresh"
            :style="{ width: '100%' }"
            class="slide-box"
            ref="slideBlock"
            :slider-text="msg"
          ></slide-verify>
          <div class="yzmBox">
            <input
              class="yzm"
              placeholder="请输入短信验证码"
              v-model.trim="smsCode"
            />
            <div class="btn" :class="isShowCount ? 'active' : ''">
              <p v-show="!isShowCount" @click="getSMScode">获取验证码</p>
              <p v-show="isShowCount">{{ count }}s</p>
            </div>
          </div>
          <div class="loginBtn" @click="login">登录</div>
        </form>
        <form v-show="!isShowForm" id="weixin">二维码登录</form>
      </div>
      <div class="quitBtn" @click="showLogin">
        <img src="../assets/img/close.png" alt="" />
      </div>
    </div>
  </div>
</template>

<script>
import { mapMutations, mapActions } from "vuex";
import { validatePhoneNum } from "@/utils";
import { SendSMS, PhoneLoginAPI, bindPhoneAPI } from "@/request/api";
export default {
  data() {
    return {
      msg: "向右滑动",
      phoneValue: "19120749754",
      isShowForm: true,
      isShowCount: false, //控制是否显示倒计时
      count: 3, //倒计时
      smsCode: "", //验证码
    };
  },
  methods: {
    ...mapMutations({
      showLogin: "handleLoginModal/showLogin",
      chanIsloginde: "loginState/chanIsloginde",
      
    }),
    ...mapActions({
      asynChanIsShowToast: "handleToast/asynChanIsShowToast",
      asyncChanUserInfo: "userInfo/asyncChanUserInfo",
    }),
    weixinClick() {
      // 点击切换微信扫码登录这一项，并向微信扫码登录
      this.isShowForm = !this.isShowForm;

      // 申请微信登录二维码
      let _this = this;
      new WxLogin({
        id: "weixin",
        appid: "wx67cfaf9e3ad31a0d", // 这个appid要填死
        scope: "snsapi_login",
        // 扫码成功后重定向的接口
        redirect_uri: "https://sc.wolfcode.cn/cms/wechatUsers/shop/PC",
        // state填写编码后的url
        state: encodeURIComponent(
          window.btoa("http://127.0.0.1:8080" + _this.$route.path)
        ),
        // 调用样式文件
        href: "data:text/css;base64,Lyogd3hsb2dpbi5jc3MgKi8NCi5pbXBvd2VyQm94IC50aXRsZSwgLmltcG93ZXJCb3ggLmluZm97DQogIGRpc3BsYXk6IG5vbmU7DQp9DQoNCi5pbXBvd2VyQm94IC5xcmNvZGV7DQogIG1hcmdpbi10b3A6IDIwcHg7DQp9",
      });
    },
    countDown() {
      let timer = setInterval(() => {
        this.count--;
        if (this.count === 0) {
          clearInterval(timer);
          this.isShowCount = !this.isShowCount;
          this.count = 3;
        }
      }, 1000);
    },
    verifyFn() {
      // 1.验证手机号码

      if (!validatePhoneNum(this.phoneValue)) {
        this.asynChanIsShowToast({
          msg: "请先输入正确的手机号",
          // warning 警告
          // success 成功
          // danger 失败
          type: "warning",
        });
        return true;
      }
      //2.
      // if (this.msg == "再试一次" || this.msg == "向右滑动") {
      //   this.asynChanIsShowToast({

      //   msg: "请进行滑块验证",
      //   // warning 警告
      //   // success 成功
      //   // danger 失败
      //   type: "warning",
      // });
      //   return true;
      // }
    },
    async getSMScode() {
      if (this.verifyFn()) return;
      //获取服务器数据
      let res = await SendSMS({
        phone: this.phoneValue,
      });
      //判断res是不是false
      if (!res) return;
      //业务成功要执行的代码
      // console.log(res);
      this.isShowCount = !this.isShowCount;
      this.countDown();
    },
    async login() {
      if (this.verifyFn()) return;

      //判断验证码有无输入
      if (!this.smsCode) {
        this.asynChanIsShowToast({
          msg: "请先输入验证码",
          // warning 警告
          // success 成功
          // danger 失败
          type: "warning",
        });
        return;
      }

      //4.发送登录请求
      let uuid = localStorage.getItem("uuid");
      let res = null;
      if (uuid) {
        //有uuid的情况
        res = await bindPhoneAPI({
          verifyCode: this.smsCode,
          phone: this.phoneValue,
          uuid,
        });
      } else {
        res = await PhoneLoginAPI({
          verifyCode: this.smsCode,
          phone: this.phoneValue,
        });
      }

      if (!res) return;

      //登录成功后的逻辑
      // console.log("登录成功", res);
      //1.提示用户登录成功
      this.asynChanIsShowToast({
        msg: "登录成功",
        // warning 警告
        // success 成功
        // danger 失败
        type: "success",
      });

      //  2.关闭登录窗口
      this.showLogin();
      //3.保存token值到localstorage中

      localStorage.setItem("x-auth-token", res["x-auth-token"]);
      //4.获取用户信息
      this.asyncChanUserInfo();
      //5.登录状态值切换，这个值影响页面信息的展示:比如用户的信息
      this.chanIsloginde();
    },
    // 拼图成功
    onSuccess(times) {
      let ms = (times / 1000).toFixed(1);
      this.msg = "login success, 耗时 " + ms + "s";
    },
    // 拼图失败
    onFail() {
      this.onRefresh(); // 重新刷新拼图
    },
    // 拼图刷新
    onRefresh() {
      this.msg = "再试一次";
    },
  },
};
</script>

<style lang = "less" scoped>
.login-modal {
  width: 100%;
  height: 100%;
  position: fixed;
  left: 0;
  top: 0;
}
.mask {
  width: 100%;
  height: 100%;
  background-color: #000;
  opacity: 0.5;
}
.loginBox {
  width: 555px;
  height: 423px;
  background: url("../assets/img/login-box-bg.png");
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto;

  /* background-color: #cfc; */
  .loginIn {
    width: 380px;
    height: 310.2px;

    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    margin: auto;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    align-items: center;
    form {
      height: 100%;
      display: flex;
      flex-direction: column;
      justify-content: space-around;
      align-items: center;
    }
    .scanCode {
      width: 360px;
      height: 40px;

      display: flex;
      justify-content: center;
      align-items: center;
      :nth-child(2) {
        padding: 0 22px;
      }
      li {
        font-size: 16px;
        color: #ccc;
        &:nth-child(2) {
          cursor: auto;
        }
      }
      .active {
        color: #333;
      }
    }
    .phoneInput {
      input {
        width: 360px;
        height: 50px;
        font-size: 16px;
        border: 1px #ccc solid;
        border-radius: 3px;
      }
    }
    .slideBox {
      background-color: #f7f9fa;
      width: 358px;
      height: 38px;
      font-weight: 500;
      color: rgb(75, 74, 74);
      line-height: 40px;
      text-align: center;
      border: 1px #e6e8eb solid;
      position: relative;
      .slide {
        width: 42px;
        height: 42px;
        background-color: #fff;
        position: absolute;
        left: 0;
        top: 0;
        box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.2);
        transition: 0.5s;
        &:hover {
          /* box-shadow: 0px 0px 5px rgba(71, 112, 226, 0.2);
          border:  1px rgba(71, 112, 226,) solid;; */
          background-color: rgba(71, 112, 226);
        }
      }
    }
    .yzmBox {
      width: 360px;
      height: 50.2px;

      display: flex;
      text-align: center;
      justify-content: space-between;
      line-height: 50.2px;
      .yzm {
        flex: 3;
        text-align: left;
        padding-left: 5px;

        border: 1px #ccc solid;
        border-radius: 3px;
      }
      .btn {
        flex: 1;
        border-radius: 3px;
        background-color: #0a328e;
        margin-left: 3px;
        cursor: pointer;
        width: 100px;
        color: white;
        &.active {
          background-color: #ddd;
          color: #000;
        }
      }
    }
    .loginBtn {
      width: 100%;
      height: 50px;
      background-color: #0a328e;
      text-align: center;
      line-height: 50px;
      color: white;
      border-radius: 5px;
      font-size: 16px;
      transition: 0.5s;
      &:hover {
        opacity: 0.7;
      }
    }
  }
  .quitBtn {
    position: absolute;
    top: 18px;
    bottom: 0;
    left: 462px;
    right: 0;
    margin: auto;
  }
}

/deep/.slide-box {
  width: 100%;
  position: relative;
  box-sizing: border-box;
  canvas {
    position: absolute;
    left: 0;
    top: -120px;
    display: none;
    width: 100%;
    box-sizing: border-box;
  }
  .slide-verify-block {
    width: 85px;
    height: 136px;
  }
  .slide-verify-refresh-icon {
    top: -120px;
    display: none;
  }
  &:hover {
    canvas {
      display: block;
    }
    .slide-verify-refresh-icon {
      display: block;
    }
  }
  .slide-verify-slider {
    margin-top: 0px;
  }
}
</style>